package com.yh.correct.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.yh.correct.model.CorrectedPerson;
import com.yh.correct.model.GroupGuardian;
import com.yh.correct.model.GroupJudicial;
import com.yh.correct.model.Guardian;
import com.yh.correct.model.Page;
import com.yh.correct.model.ResultData;
import com.yh.correct.service.IGroupService;
/**
 * 工作管理模块的帮教小组控制层
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/groupJG")
public class GroupJGController {
	
	private static Logger logger = LogManager.getLogger(GroupJGController.class);
	@Autowired
	private IGroupService groupService;
	
	
	/**
	 * 根据矫正人员姓名、证件号码及所属单位分页模糊查询
	 * @param pageSize
	 * @param pageNum
	 * @param correctedPerson
	 * @return
	 */
	@RequestMapping("/querylike")
	public ResultData queryLikeGroups(int pageSize, int pageNum,CorrectedPerson correctedPerson){ 
		logger.info("矫正人查询条件"+correctedPerson);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pageSize", pageSize);
		map.put("pageNum", pageNum);
		map.put("correctedPerson",correctedPerson);
		Page page=groupService.queryLikeCorrectedPersons(map);
		List<Object> list=new ArrayList<Object>();
		list.add(page);
		ResultData rd=new ResultData();
		rd.setCode(0);
		rd.setMessage("查询成功！");
		rd.setData(list);
		return rd;
	}
	
	
	
	/**
	 * 根据矫正对象id查找其小组信息
	 * @param id
	 * @return
	 */
	@RequestMapping("/tolook")
	public ModelAndView getGroups(int id){ 
		logger.info("查询帮教小组"+id);
		List<Object> list=groupService.getGroups(id);
		return new ModelAndView("work-manage/group_look","groups",list);
	}
	
	/**
	 * 更新帮教小组
	 * @param cid
	 * @param pList
	 * @param gList
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/update")
	public ResultData updateGroups(int cid,@RequestParam(value="pList[]",required=false) List<Object> pList,@RequestParam(value="gList[]",required=false) List<Object> gList) throws Exception{ 
		logger.info("更新帮教小组"+cid+pList+gList);
		List<GroupJudicial> list1=new ArrayList<GroupJudicial>();
		if(pList!=null){
			for(Object n : pList){
				GroupJudicial groupJudicial=new GroupJudicial();
				groupJudicial.setCid(cid);
				groupJudicial.setDid(Integer.parseInt(n.toString()));
				list1.add(groupJudicial);
			}
		}
		List<GroupGuardian> list2=new ArrayList<GroupGuardian>();
		if(gList!=null){
			for(Object n : gList){
				GroupGuardian groupGuardian=new GroupGuardian();
				groupGuardian.setCid(cid);
				groupGuardian.setGid(Integer.parseInt(n.toString()));
				list2.add(groupGuardian);
			}
		}
		groupService.updateGroups(cid,list1,list2);
		ResultData rd=new ResultData();
		rd.setCode(0);
		rd.setMessage("修改成功！");
		return rd;
	}

	/**
	 * 根据矫正对象id查找其小组信息
	 * @param id
	 * @return
	 */
	@RequestMapping("/get")
	public ResultData getGroups2(int id){ 
		logger.info("查询帮教小组"+id);
		List<Object> list=groupService.getGroups(id);
		ResultData rd=new ResultData();
		rd.setCode(0);
		rd.setMessage("查询成功！");
		rd.setData(list);
		return rd;
	}
	
	/**
	 * 增加监护人时先查询监护人
	 * @param guardian
	 * @return
	 */
	@RequestMapping("/getGuardian")
	public ResultData getGuardian(Guardian guardian){
		logger.info("增加监护人时先查询监护人"+guardian);
		List<Object> list=groupService.getGuardian(guardian);
		ResultData rd=new ResultData();
		rd.setCode(0);
		rd.setMessage("查询成功！");
		rd.setData(list);
		return rd;
	}
	
	/**
	 * 返回页面通用方法
	 * @param page
	 * @return
	 */
	@RequestMapping("/page")
	public ModelAndView page(String page){
		return new ModelAndView(page);
	}
	
	
}
